/*
* $SnapStarLicense:
* 
*
* SnapLogic - Data Integration 
*
* Copyright (C) 2007 - 2013, SnapLogic, Inc.  All rights reserved.
*
* This program is licensed under the terms of 
* the SnapLogic Commercial Subscription agreement. 
*
* "SnapLogic" is a trademark of SnapLogic, Inc.
*
*
*
*
*
*
*
*
* $
*
* $Id: FileSizeFormatter.as  $
*/

package components
{
	import mx.formatters.Formatter;
	
	public class FileSizeFormatter extends Formatter
	{
		private static const LEVELS:Array = ['bytes', 'Kb', 'MB', 'GB', 'TB', 'PB'];
		
		/*
		 * Convert filesize to nearest KB, MB, GB etc
		*/
		override public function format(value:Object):String
		{
			var bytes:Number = parseFloat( value.toString() );
			if ( isNaN(bytes) ){
				//exit
				return value.toString();
			}
			
			var index:uint = Math.floor( Math.log(bytes)/Math.log(1024) );
			return (bytes/Math.pow(1024, index)).toFixed(2) + LEVELS[index];
		}
	}
}